package workflow.action;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;
import org.springside.core.web.StrutsEntityAction;

import workflow.dao.ProcessNodeDao;
import workflow.model.ProcessNode;
import workflow.service.ProcessService;
import workflow.util.ProcessEngine;

import common.Globals;
import common.PageInfo;

//流程节点类
public class ProcessNodeAction extends StrutsEntityAction<ProcessNode, ProcessNodeDao> {
	private ProcessNodeDao processNodeDao;
	
	public ProcessNodeDao getProcessNodeDao() {
		return processNodeDao;
	}

	public void setProcessNodeDao(ProcessNodeDao processNodeDao) {
		this.processNodeDao = processNodeDao;
	}

	/**
	 *列表
	 */
	@Override
	public ActionForward list(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		LazyValidatorForm lvForm = (LazyValidatorForm)form;
		PageInfo pageInfo = new PageInfo(request);
		HashMap<String, String> hm = new HashMap<String, String>();
		//TODO 组装查询条件
		
		String column1 = (String)lvForm.get("s_id");
		hm.put("id" ,column1);
		String proInsId = (String)lvForm.get("s_proInsId");
		hm.put("proInsId" ,proInsId);
		String name = (String)lvForm.get("s_name");
		hm.put("name" ,name);
		String assignee = (String)lvForm.get("s_assignee");
		hm.put("assignee" ,assignee);
		String formUrl = (String)lvForm.get("s_formUrl");
		hm.put("formUrl" ,formUrl);
		String createDate = (String)lvForm.get("s_createDate");
		hm.put("createDate" ,createDate);
		String dueDate = (String)lvForm.get("s_dueDate");
		hm.put("dueDate" ,dueDate);
		String hbTask = (String)lvForm.get("s_hbTask");
		hm.put("hbTask" ,hbTask);
		String nodeFlag = (String)lvForm.get("s_nodeFlag");
		hm.put("nodeFlag" ,nodeFlag);
		String nodeType = (String)lvForm.get("s_nodeType");
		hm.put("nodeType" ,nodeType);
		String expression = (String)lvForm.get("s_expression");
		hm.put("expression" ,expression);
		String status = (String)lvForm.get("s_status");
		hm.put("status" ,status);
		String visiable = (String)lvForm.get("s_visiable");
		hm.put("visiable" ,"1");

				
		List<ProcessNode> poList = this.processNodeDao.findList(hm, pageInfo);
		request.setAttribute("list", poList);
		
		return mapping.findForward(Globals.FORWARD_LIST);
	}
	
	/**
	 * 删除
	 */
	@Override
	public ActionForward delete(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		super.delete(mapping, form, request, response);
		
		return this.list(mapping, form, request, response);
	}

	/**
	 * 保存
	 */
	@Override
	public ActionForward save(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {		
		super.save(mapping, form, request, response);
		
		return this.list(mapping, form, request, response);
	}
	
	/**
	 * 该方法被delete方法调用
	 */
	@Override
	protected void doDeleteEntity(ActionForm form, HttpServletRequest request) {
		String ids = request.getParameter("ids");
		try {
			if (ids != null && !"".equals(ids.trim())) {
				getEntityManager().deleteByIds(ids);
				// TODO 修改操作事件，参考logWrite方法要求
				//logsManager.logWrite(request, "", "操作日志", 0);
			}
		} catch (Exception e) {
			// TODO 修改操作事件，参考logWrite方法要求
			//logsManager.logWrite(request, "", "操作日志", 0);
		}
	}
	
	public ActionForward executeTask(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {		
		try {
			//=============================完成节点任务================================start
			//流程节点ID
			String taskId = request.getParameter("id");
			//流程变量表，变量名必须和流程设置的变量名一致。
			HashMap<String, String> variableMap = new HashMap<String, String>();
			
			variableMap.put("JIFANG", "1");//-机房
			variableMap.put("JICHU", "1");//-基础 
			variableMap.put("DIANLI", "1");//-电力
			variableMap.put("DIANYUAN", "0");//-电源 
			variableMap.put("DIWANG", "0");//-地网 
			variableMap.put("DONGHUAN", "1");//-动环 
			variableMap.put("GANTA", "0");//-杆塔 
			variableMap.put("TIETA", "0");//-杆塔
			
			variableMap.put("KONGTIAO", "1");//-空调 
			variableMap.put("AC", "0");//-AC屏 
			variableMap.put("TUJIAN", "0");//-土建 
			variableMap.put("CHUANSHU", "1");//-传输

			variableMap.put("CHUANSHUGUANDAO", "1");//-传输管道 
			variableMap.put("CHUANSHUXIANLU", "1");//-传输线路 
			variableMap.put("CHUANSHUSHEBEI", "0");//-传输设备 
			variableMap.put("JIFANGJIANSHE", "0");//-机房建设 
			variableMap.put("JIFANGZHUANGXIU", "1");//-机房装修
			
			variableMap.put("isback", "1");
			
			//完成节点，触发下一节点
			ProcessEngine rrocessEngine = ProcessEngine.getInstance(request);
			ProcessService processManager = rrocessEngine.getProcessService();
			processManager.completeTask(taskId, variableMap, null);			

			//建立工单
			//build Bill by Task
			//==============================完成节点任务==============================end
			System.out.println("配套工单：" + processManager.getBillTypes(Long.valueOf(taskId)));

			ProcessNode task = this.processNodeDao.findUniqueBy(ProcessNode.class, "id", Long.valueOf(taskId));

			ActionForward af = new ActionForward();
			af.setPath("/ProcessNodeAction.do?method=list&s_proInsId=" + task.getProInsId());
			af.setRedirect(true);

			return af;
		} catch (Exception e) {
			e.printStackTrace();
			return this.list(mapping, form, request, response);
		}
	}
	
	public ActionForward executeApplyTask(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response){
		try {
			//=============================完成节点任务================================start
			//流程节点ID
			String taskId = request.getParameter("id");
			//流程变量表，变量名必须和流程设置的变量名一致。
			HashMap<String, String> variableMap = new HashMap<String, String>();
			
			variableMap.put("JIANLI", "1");//-监理
			variableMap.put("XIANGMUFUZEREN", "1");//-项目负责人
			variableMap.put("SHIJINGLI", "0");//-室经理 
			
			//完成节点，触发下一节点
			ProcessEngine rrocessEngine = ProcessEngine.getInstance(request);
			ProcessService processManager = rrocessEngine.getProcessService();
			processManager.completeTask4Apply(taskId, variableMap, null);

			//建立工单
			//build Bill by Task
			//==============================完成节点任务==============================end
			HashSet set = new HashSet();
			set.add("JIANLI");
			set.add("XIANGMUFUZEREN");
			HashMap map = (HashMap)processManager.getVariables(taskId, set);

			ProcessNode task = this.processNodeDao.findUniqueBy(ProcessNode.class, "id", Long.valueOf(taskId));

			ActionForward af = new ActionForward();
			af.setPath("/ProcessNodeAction.do?method=list&s_proInsId=" + task.getProInsId());
			af.setRedirect(true);

			return af;
		} catch (Exception e) {
			e.printStackTrace();
			return this.list(mapping, form, request, response);
		}
	}
}


